<template>
  <div class="mod-housingEstate">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-button v-if="superAdmin == 1"  type="primary" @click="addOrUpdateHandle()">新增小区</el-button>
       </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
          v-if='show'
          prop="id"
          header-align="center"
          align="center"
          width="80"
          label="ID">
      </el-table-column>
      <el-table-column
          type="index"
         :label="$t('tabble.No')"
          width="50"
          :index="indexMethod">
      </el-table-column>
      <el-table-column
        prop="fullName"
        header-align="center"
        align="center"
        label="小区全称">
      </el-table-column>
      <el-table-column
        prop="shortName"
        header-align="center"
        align="center"
        label="简称">
      </el-table-column>
      <el-table-column
        prop="code"
        header-align="center"
        align="center"
        label="小区编码">
      </el-table-column>
       <el-table-column
        prop="proviceName"
        header-align="center"
        align="center"
        label="省">
      </el-table-column>
       <el-table-column
        prop="cityName"
        header-align="center"
        align="center"
        label="市">
      </el-table-column>
      <el-table-column
        prop="areaName"
        header-align="center"
        align="center"
        label="区">
      </el-table-column>
      <el-table-column
        prop="communityCodeName"
        header-align="center"
        align="center"
        label="所属社区">
      </el-table-column>
      <el-table-column
        prop="neighborhoodCommitteeCodeName"
        header-align="center"
        align="center"
        label="所属居委会">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        :label="$t('handle.handle')">
        <template slot-scope="scope">
          <el-button v-if="superAdmin == 0" type="text" size="small">{{ $t('handle.detail') }}</el-button>
          <el-button v-if="superAdmin == 1"  type="text" size="small" @click="addOrUpdateHandle(scope.row.id)" >{{ $t('handle.update') }}</el-button>
          <el-button v-if="superAdmin == 1" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('handle.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>
<script>
 import AddOrUpdate from './housingEstate-add-or-update'
export default {
    data(){
      return{
        superAdmin: sessionStorage.getItem("superAdmin"),
        dataForm: {},
        address: [],
        options: [],
        props: {
          value: "id",
          label: "name",
          children: "children"
        },
        show: false,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        dicts:[],
        dictType:{1:'未删除'},
        departmentOptions: [],
        cascaderData: [],
        type: ''
      }
    },
    computed: {
    dictTypeFilter () {
      return function (type) {
            /** do something */
            return this.dictType[type]
        }
    }
  },
    // 引入的组件
    components: {
      AddOrUpdate
    },
    created() {
      this.$http({
        url: 'dg-ic/ic/dict/type/del_status',
        method: 'get'
        }).then(({data})=>{
            if(data && data.code === 0){
              this.dicts = data.data;
              }
           })

    },
    mounted() {
      // this.getNodes()
    },
    // 一进页面就会执行的方法
    activated(){
      // this.getDataList()
      // this.getCountry()
    },
    methods: {
       indexMethod(index){
        return index+1;
      },
      // 获取数据列表
      getDataList(housId,type){
        console.log('获取小区详情',housId,type)
        this.type = type
        this.dataListLoading = true
        this.$http({
          url: `/dg-ic/ic/housing-estate/list`,
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            // 'housingId': housId
            // 后续如果有省市区继续加参数
          })
        }).then(({data})=>{
          if(data && data.code === 0){
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount

          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val){
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val){
        this.pageIndex = val
        this.getDataList()
      },

      // 新增/修改
      addOrUpdateHandle(id){
        this.addOrUpdateVisible = true
        this.$nextTick(()=>{
          this.$refs.addOrUpdate.init(id,this.type)
        })
      },
      // 删除
      deleteHandle(id){
        // 遍历拿到选择的id
        var ids = id ? [id] : this.dataListSelections.map(item=>{
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`,'提示',{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          this.$http({
            url: `/dg-ic/ic/housing-estate/delete`,
            method: 'delete',
            data: this.$http.adornData(ids,false)
          }).then(({data})=>{
            if(data && data.code === 0){
              this.$message({
                message: '删除成功',
                type: 'success',
                duration: 1500,
                onClose: ()=>{
                  this.getDataList()
                }
              })
            }else{
              this.$message.console.error(data.msg);
            }
          })
        }).catch(()=>{})
      },
      // 国家
      // getCountry(){
      //     this.$http({
      //       url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
      //       method: 'post',
      //       params:{parentId: 0,type:11}
      //     }).then(({data})=>{
      //         if(data && data.code === 0){
      //           data.data.map(item => {
      //                   this.$set(item, "name", item.name);
      //                   this.$set(item, "children", []);
      //               });
      //             this.options = data.data
      //         }
      //     })
      // },
      // 获取省份
      // getProviceCode(countryId){
      //       console.log(countryId)
      //       this.$http({
      //         url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
      //         method: 'post',
      //         params:{parentId: countryId,type:12}
      //       }).then(({data})=>{
      //           if(data && data.code === 0){
      //               data.data.map(item => {
      //                   this.$set(item, "name", item.name);
      //                   this.$set(item, "children", []);
      //               });
      //              this.options.map(item=>{
      //                  if(item.id === countryId){
      //                      item.children = data.data
      //                  }
      //              })
      //           }
      //       })
      // },
      // 获取市
      // getCityCode(countryId,provinceId){
      //       console.log(countryId,provinceId)
      //       this.$http({
      //         url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
      //         method: 'post',
      //         params:{parentId: provinceId,type:13}
      //       }).then(({data})=>{
      //           if(data && data.code === 0){
      //               data.data.map(item => {
      //                   this.$set(item, "name", item.name);
      //                   // this.$set(item, "children", []);
      //               });
      //              this.options.map((item, i) => {
      //                   if (item.id === countryId) {
      //                       item.children.map((city, id) => {
      //                            city.children = data.data;
      //                       });
      //                   }
      //               });
      //           }
      //       })
      // },
      // 获取区
      // getAreaCode(countryId,provinceId,cityId){
      //       console.log(provinceId,cityId)
      //       this.$http({
      //           url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
      //           method: 'post',
      //           params:{parentId: cityId,type:13}
      //       }).then(({data})=>{
      //           if(data && data.code === 0){
      //               data.data.map(item => {
      //                   this.$set(item, "name", item.name);
      //                   // this.$set(item, "children", []);
      //               });
      //               this.options.map((item, i) => {
      //                   if (item.id === countryId) {
      //                   item.children.map((provice, idx) => {
      //                       if(provice.id === provinceId){
      //                           provice.children.map((city, ids) => {
      //                               city.children = data.data
      //                           });
      //                       }
      //                   });
      //                   }
      //               });
      //           }

      //       })
      // },
      // getNodes(val){
      //   console.log(val)
      //   if(val.length === 1){// 获取省
      //           this.getProviceCode(val[0])
      //     }else if(val.length === 2){// 根据省份获取市
      //           this.getCityCode(val[0],val[1])
      //     }else if(val.length === 3){// 区
      //           this.getAreaCode(val[0],val[1],val[2])
      //     }
      // }
  }
}
</script>

